home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / singplay.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  5KB  |  156 lines

  1. /* single playfield example v1.0 */
  2. /****************************************************************
  3. *                                                               *
  4. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  5. * No part of this program may be reproduced, transmitted,       *
  6. * transcribed, stored in retrieval system, or translated into   *
  7. * any language or computer language, in any form or by any      *
  8. * means, electronic, mechanical, magnetic, optical, chemical,   *
  9. * manual or otherwise, without the prior written permission of  *
  10. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  11. * Los Gatos, CA 95030                                           *
  12. *                                                               *
  13. ****************************************************************/
  14.  
  15.  
  16. #include <exec/types.h>
  17. #include <graphics/gfx.h>
  18. #include <hardware/dmabits.h>
  19. #include <hardware/custom.h>
  20. #include <graphics/gfxmacros.h>
  21. #include <graphics/rastport.h>
  22. #include <graphics/view.h>
  23. #include <exec/exec.h>
  24.  
  25.  
  26. #define DEPTH 2  
  27. #define WIDTH 320 
  28. #define HEIGHT 200 
  29. #define NOT_ENOUGH_MEMORY -1000
  30. #define FOREVER for(;;) 
  31.         /* construct a simple display */ 
  32.  
  33. struct View v;
  34. struct ViewPort vp;
  35. struct ColorMap *cm;    /* pointer to colormap structure, dynamic alloc */
  36. struct RasInfo ri;
  37. struct BitMap b;
  38. struct RastPort rp;
  39.  
  40. short i,j,k,n;
  41. struct ColorMap *GetColorMap();
  42. struct GfxBase *GfxBase;
  43.  
  44. SHORT  boxoffsets[] = { 802, 2010, 3218 };
  45. USHORT colortable[] = { 0x000, 0xf00, 0x0f0, 0x00f };
  46.                         /* black, red, green, blue */
  47. UBYTE *displaymem;
  48. UWORD *colorpalette;
  49.  
  50. main()
  51. {
  52.         GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  53.         if (GfxBase == NULL) exit(1);
  54.  
  55.                                 /* initialize view */
  56.         InitView(&v);
  57.                                 /* link view into viewport */
  58.         v.ViewPort = &vp;
  59.                                 /* init view port */
  60.         InitVPort(&vp);
  61.                                 /* now specify critical characteristics */
  62.         vp.DWidth = WIDTH;
  63.         vp.DHeight = HEIGHT;
  64.         vp.RasInfo = &ri;
  65.                                 /* init bit map (for rasinfo and rastport) */
  66.         InitBitMap(&b,DEPTH,WIDTH,HEIGHT);
  67.                                 /* (init RasInfo) */
  68.         ri.BitMap = &b;
  69.         ri.RxOffset = 0;        /* align upper left corners of display
  70.                                  * with upper left corner of drawing area */
  71.         ri.RyOffset = 0;
  72.         ri.Next = NULL;
  73.                                 /* (init color table) */
  74.         cm = GetColorMap(4);    /* 4 entries, since only 2 planes deep */
  75.         colorpalette = cm->ColorTable;
  76.         for(i=0; i<4; i++)
  77.                 *colorpalette++ = colortable[i];
  78.                                 /* copy my colors into this data structure */
  79.         vp.ColorMap = cm;       /* link it with the viewport */
  80.  
  81.                                  /* allocate space for bitmap */
  82.         for(i=0; i<DEPTH; i++)
  83.            {
  84.            b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  85.            if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  86.            }
  87.  
  88.         MakeVPort( &v, &vp );   /* construct copper instr (prelim) list */
  89.         MrgCop( &v );           /* merge prelim lists together into a real 
  90.                                  * copper list in the view structure. */
  91.  
  92.         for(i=0; i<2; i++)
  93.                 {
  94.                 displaymem = (UBYTE *)b.Planes[i];
  95.                 for(j=0; j<RASSIZE(WIDTH,HEIGHT); j++)
  96.                         *displaymem++ = 0;      
  97.                 /* zeros to all bytes of the display area */                                    }
  98.  
  99.         LoadView(&v);
  100.  
  101.         /* now fill some boxes so that user can see something */
  102.         /* always draw into both planes to assure true colors */
  103.  
  104.         for(n=1; n<4; n++)      /* three boxes */
  105.         {
  106.                 for(k=0; k<2; k++)
  107.                 {
  108.                 displaymem = b.Planes[k] + boxoffsets[n-1];  
  109.                 DrawFilledBox(n,k);     
  110.                 /* boxes will be in red, green and blue */
  111.                 }       
  112.         }
  113.  
  114.         FOREVER
  115.            ;            /* do nothing till user resets machine */ 
  116.  
  117.         FreeMemory();   /* discussion purposes only, user has no 
  118.                          * way here to get past "FOREVER" in this
  119.                          * simple program.  FreeMemory is discussed
  120.                          * in section below... Exiting Gracefully */
  121.  
  122. }       /* end of main() */
  123.  
  124. DrawFilledBox(fillcolor,plane)
  125. SHORT fillcolor,plane;
  126. {
  127.         UBYTE value;
  128.         
  129.         for(j=0; j<100; j++)
  130.            {
  131.            if((fillcolor & (1 << plane)) != 0)
  132.                 value = 0xff;
  133.            else
  134.                 value = 0;
  135.            for(i=0; i<20; i++)
  136.                 {
  137.                 *displaymem++ = value;
  138.                 }
  139.            displaymem += (b.BytesPerRow - 20);
  140.            }
  141.         return;
  142. }
  143.  
  144. FreeMemory()
  145. {               /* return user and system-allocated memory to sys manager */
  146.  
  147.         for(i=0; i<DEPTH; i++)                  /* free the drawing area */
  148.            FreeRaster(b.Planes[i],WIDTH,HEIGHT);
  149.         FreeColorMap(cm);                       /* free the color map */
  150.                 /* free dynamically created structures */
  151.         FreeVPortCopLists(&vp);                 
  152.         FreeCprList(&v.LOFCprList);
  153.         return;
  154. }       
  155.  
  156.